<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="月份" prop="configName">
        <el-date-picker
            v-model="queryParams.yearMonth"
            type="month"
            value-format="YYYY-MM"
            placeholder="选择月份"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户" prop="configName">
        <el-input
            v-model="queryParams.userName"
            placeholder="请输入用户"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Plus"
            @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户" align="center" prop="userName" />
      <el-table-column label="补卡时间" align="center" prop="clocktime" />
      <el-table-column label="类型" align="center" prop="ftype">
        <template #default="scope">
          <span v-if="scope.row.ftype === 1">补卡</span>
          <span v-else-if="scope.row.ftype === 2">请假</span>
          <span v-else-if="scope.row.ftype === 3">出差</span>
        </template>
      </el-table-column>
      <el-table-column label="请假类型" align="center" prop="qingjiatype" />
      <el-table-column label="记录创建时间" align="center" prop="fcreatetime" />
      <el-table-column label="来源" align="center" prop="fromsource">
        <template #default="scope">
          <span v-if="scope.row.fromsource === 1">系统生成</span>
          <span v-else-if="scope.row.fromsource === 2">手动生成</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" min-width="200" />
      <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pn"
        v-model:limit="queryParams.ps"
        @pagination="getList"
    />
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog title="新增" v-model="open" width="500px" append-to-body>
      <el-form ref="configRef" :model="form" label-width="80px" size="default">
        <el-form-item label="补卡时间" prop="clocktime">
          <el-date-picker
              v-model="form.clocktime"
              type="datetime"
              value-format="YYYY-MM-DD HH:mm:ss"
              placeholder="选择补卡时间"
          />
        </el-form-item>
        <el-form-item label="用户" prop="userid">
          <el-input v-model="form.user" placeholder="请选择用户">
            <template #append>
              <el-button :icon="Search" @click="isUserVis=true" />
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="类型" prop="remark">
          <el-select v-model="form.ftype" placeholder="请输入内容">
            <el-option label="补卡" :value="1" />
            <el-option label="请假" :value="2" />
            <el-option label="出差" :value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="请假类型" prop="qingjiatype">
          <el-input v-model="form.qingjiatype" placeholder="请输入请假类型" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <userlist v-if="isUserVis" @user="queryUser" @close="isUserVis=false" />
  </div>
</template>

<script setup name="ClockRecord">
import { queryClockRecordList } from "@/api/system/config";
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import userlist from "@/views/system/dept/userlist";
import {addClockRecord, delClockRecord} from "../../../api/system/config.js";
const { proxy } = getCurrentInstance();
const { sys_yes_no } = proxy.useDict("sys_yes_no");
import { Search } from '@element-plus/icons-vue'
const configList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const open = ref(false);
const isUserVis = ref(false);
const time = ref(false);
const total = ref(0);

const data = reactive({
  form: {},
  queryParams: {
    pn: 1,
    ps: 10,
    yearMonth: '',
    userName: undefined
  }
});

const { queryParams, form } = toRefs(data);

/** 查询参数列表 */
function getList() {
  loading.value = true;
  const DATA = {
    ...queryParams.value,
    fyear: queryParams.value.yearMonth?.split('-')[0],
    fmonth: queryParams.value.yearMonth?.split('-')[1]
  }
  queryClockRecordList(DATA).then(response => {
    configList.value = response.data.list
    total.value = response.data.total
    loading.value = false;
  });
}
/** 新增 */
function handleAdd() {
  form.value = {
    user: '',
    userid: '',
    ftype: 1,
    clocktime: '',
    fromsource: 2,
    remark: '',
    qingjiatype: ''
  };
  open.value = true
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["configRef"].validate(valid => {
    if (valid) {
      addClockRecord(form.value).then(response => {
        proxy.$modal.msgSuccess("新增成功");
        open.value = false;
        getList();
      });
    }
  });
}
/** 获取负责人 */
function queryUser(ev) {
  const { userName, userId } = ev
  form.value = {
    ...form.value,
    user: userName,
    userid: userId
  }
  isUserVis.value = false
}
/** 取消按钮 */
function cancel() {
  open.value = false;
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pn = 1;
  getList();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.recordId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 删除按钮操作 */
function handleDelete(row) {
  const idList = (row.recordId && [row.recordId]) || ids.value;
  proxy.$modal.confirm('是否确认删除参数编号为"' + idList + '"的数据项？').then(function () {
    return delClockRecord({idList});
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 导出按钮操作 */
function handleExport() {
  const tHeader = [
    '用户', '补卡时间', '类型', '请假类型', '记录创建时间', '来源', '备注',
  ]
  const filterVal = [
    'userName', 'clocktime', 'ftype', 'qingjiatype', 'fcreatetime', 'fromsource', 'remark',
  ]
  const DATA = {
    ...queryParams.value,
    ps: 100000,
    fyear: queryParams.value.yearMonth?.split('-')[0],
    fmonth: queryParams.value.yearMonth?.split('-')[1]
  }
  queryClockRecordList(DATA).then(response => {
    let list = response.data.list
    list.map(res => {
      if(res.ftype === 1) {
        res.ftype = '补卡'
      } else if(res.ftype === 2) {
        res.ftype = '请假'
      } else if(res.ftype === 3) {
        res.ftype = '出差'
      }
      if(res.fromsource === 1) {
        res.fromsource = '系统生成'
      } else if(res.fromsource === 2) {
        res.fromsource = '手动生成'
      }
    })
    const data = formatJson(filterVal, list)
    export_json_to_excel(tHeader, data, '补卡记录表')
  });
}

getList();
</script>
